<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'Hack assembler',
	'<{body}>' => <<<END
<section id="include.d">
	<h2>include.d version 0.0.1.7</h2>
	<p>
		Ugh.
		I don&apos;t feel well today.
		My faeces have been becoming increasingly liquidy, until today, I swear they were mostly water.
		And now I have a stomachache, can&apos;t keep hydrated, and am having difficulties focussing on my studies.
		I don&apos;t have time for this.
		Like last time, my dental appointment lands on my day off, the final day of the school week.
		I also spent much of yesterday, my other day off for the week, trying to find my way to the dentists&apos; office to even make the appointment.
		I need to keep on task.
		The workload this week is lower than normal, at least theoretically, but I&apos;m required to use an assembler that I was never able to get running.
		The professor has offered no help whatsoever, despite my several mentions to them that I couldn&apos;t get it working.
		That said, I didn&apos;t actually <strong>*ask*</strong> for help, so I can&apos;t complain too much, but I don&apos;t really feel like asking for help from someone that represents a school that doesn&apos;t seem to care.
	</p>
	<p>
		Anyway, to replace the assembler I have to use, I&apos;ve built my own assembler.
		It certainly enhanced my understanding of assemblers, but it also took a lot of time that I feel I don&apos;t have.
		Well, sort of don&apos;t have.
		As long as I keep focussed, this shouldn&apos;t be a problem.
	</p>
	<p>
		As a side note, it&apos;s interesting to me that the school&apos;s censorship rules can be applied in either direction.
		For example, I&apos;ve already posted my assembler publicly.
		This is a fact.
		However, we were never required to build it, assuming we got the supplied assembler working.
		That means that as long as I don&apos;t include my assembler in anything I submit to the school, I haven&apos;t broken any rules.
		However, if I <strong>*do*</strong> include it in my learning journal or in the discussion forum, even though my public posting of it will have come first, I&apos;ll be in violation of the rules.
		That means that to avoid trouble, I have to censor <strong>*what I&apos;m going to submit to the school*</strong>.
		I imagine that&apos;s not at all how they intended their rule, but it certainly will keep me out of trouble.
	</p>
	<p>
		Anyway, as I said, I&apos;ve posted my assembler publicly.
		The bulk of the work is done by a pair of new classes in my new <a href="https://notabug.org/y.st./include.d/releases">include.d release</a>, version 0.0.1.7.
		The rest of the work is done by this short script here:
	</p>
	<blockquote>
<pre><code>&lt;?php declare(strict_types=1);
/**
 * Copyright © 2017 Alex Yst &lt;mailto:copyright@y.st&gt;
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see &lt;http://www.gnu.org./licenses/&gt;.
**/

// Causes all errors to be terminal
require &apos;st/y/function/error_handler.php&apos;;
set_error_handler(&apos;\\\\st\\\\y\\\\error_handler&apos;);
// Sets the script up to used the classes from include.d
spl_autoload_register();

\$input = file_get_contents(\$argv[1]);

\$symbol = new \\st\\y\\hack\\symbol_table();
\$lines = explode(&quot;\\n&quot;, \$input);
\$address = 0;

foreach(\$lines as \$l =&gt; &amp;\$line):
	\$comment = strpos(\$line, &apos;//&apos;);
	if(\$comment !== false):
		\$line = substr(\$line, 0, \$comment);
	endif;
	\$line = preg_replace(&apos;{\\\\s}&apos;, &apos;&apos;, \$line);
	\$length = strlen(\$line);
	if(\$line == &apos;&apos;):
		unset(\$lines[\$l]);
	elseif(\$line[0] == &apos;(&apos; and \$line[\$length-1] == &apos;)&apos;):
		\$symbol[substr(\$line, 1, -1)] = \$address;
		unset(\$lines[\$l]);
	else:
		\$address++;
	endif;
endforeach;
unset(\$line);

foreach(\$lines as \$line):
	echo new \\st\\y\\hack\\instruction(\$line, \$symbol), &quot;\\n&quot;;
endforeach;</code></pre>
	</blockquote>
</section>
END
);
